neutron.setTitle("Open File")


window.onload = function () {
    var path = location.hash.replace("#", "")
    console.log("Open file: " + path)
    var xhr = new XMLHttpRequest();
    xhr.open("GET", API_PATH + "/open?path=" + path, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            if (data.code == 0) {
                if (data.type == "open") { // 打开
                    eval(data.data)
                    setTimeout(() => {
                        neutron.close()
                    }, 300)
                } else {
                    neutron.setMinSize(240, 340) // 缩放
                    neutron.setWinSize(240, 340)
                    load_opentypes(data.data)
                }
            } else {
                alert(data.msg)
                setTimeout(() => {
                    neutron.close()
                }, 300)
            }
        } else if (xhr.readyState == 4) {
            setTimeout(() => {
                neutron.close()
            }, 300)
        }
    }
    xhr.send()
}
function load_opentypes(data) { // 加载打开方式
    var root_elem = document.getElementById("root")
    for (let key in data) {
        var template = `
        <div class="open_type" id="open_type-`+ key + `">
            <img alt="" src="`+ data[key].icon + `">
            <span>`+ data[key].name + `</span>
        </div>`
        root_elem.insertAdjacentHTML("beforeend", template);
        (function (codes) {
            document.getElementById("open_type-" + key).onclick = function () {
                eval(codes)
                neutron.close()
            }
        })(data[key].code)
    }
}